<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use \think\Request;
use \think\Session;
use app\common\model\TemplateConfig;
use app\common\model\MsgAndMailConfig as MsgAndMailConfigModel;

/**
 * 短信邮件配置
 * Class MsgAndMailConfig
 * @package app\admin\controller
 */
class MsgAndMailConfig extends AdminBase
{
	/**
	 * 模板列表页面
	 */
	public function index()
	{
		$list = TemplateConfig::paginate(10);
		$page = $list->render();
		$this->assign('list', $list);
		$this->assign('page', $page);
		return $this->fetch();
	}

	/**
	 * 添加模板页面
	 */
	public function add()
	{
		return $this->fetch();
	}

	/**
	 * 短信配置页面
	 */
	public function message()
	{
		$this->assign('config_info', MsgAndMailConfigModel::get(['type' => 0]));
		return $this->fetch();
	}

	/**
	 * 邮件配置页面(暂未配置)
	 */
	public function mail()
	{
		return $this->fetch();
	}

	/**
	 * 处理添加模板
	 */
	public function doAdd()
	{	
		$info = Request::instance()->post();
		$info['create_time'] = date('Y-m-d H:i:s');
		$info['create_user'] = Session::get('admin_name');
		if (TemplateConfig::create($info)) {
            $action = '模板';
            $this->getLog("1",$action);
			$this->success('添加成功');
		} else {
			$this->error('添加失败');
		}
	}

	/**
	 * 处理保存配置（邮件配置暂时没做）
	 */
	public function save()
	{	
		$info = Request::instance()->post();

		if ($result = MsgAndMailConfigModel::get(['type' => $info['type']])) {
			$result->url = $info['url'];
			$result->username = $info['username'];
			$result->password = $info['password'];
			$result->update_user = Session::get('admin_name');
			$result->update_time = date('Y-m-d H:i:s');
			if ($result->save()) {
			    $action = '短信邮件：'.$_REQUEST['username'];
			    $this->getLog("1",$action);
				$this->success('更新成功');
			} else {
				$this->error('更新失败');
			}
		} else {
			if ($info['type'] == 0)	{
				$info['update_user'] = Session::get('admin_name');
				$info['create_time'] = date('Y-m-d H:i:s');
				if (MsgAndMailConfigModel::create($info)) {
				    $action = '短信邮件';
				    $this->getLog("1",$action);
					$this->success('保存成功');
				} else {
					$this->error('保存失败');
				}

			} elseif ($info['type'] == 1) {
				$info['update_user'] = Session::get('admin_name');
				$info['create_time'] = date('Y-m-d H:i:s');
				if (MsgAndMailConfigModel::create($info)) {
				    $action = '短信邮件';
				    $this->getLog("1",$action);
					$this->success('保存成功');
				} else {
					$this->error('保存失败');
				}

			} else {
				$this->error('访问出错');
			}
		}
	}

	/**
	 * 删除模板配置
	 */
	public function delete($id = '')
	{	
		if (TemplateConfig::destroy($id)){
            $action = '模板配置ID'.$id;
            $this->getLog("2",$action);
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}

	/**
	 * 启用、禁用模板
	 */
	public function status($id = '', $status = '')
	{
		if (TemplateConfig::update(['id' => $id, 'status' => $status])) {
            $action = '模板'.$_REQUEST['title'];
            $this->getLog("4",$action);
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}

	/**
	 * 编辑模板页面
	 */
	public function edit($id = '')
	{
		$info = TemplateConfig::get($id);
		return $this->fetch('edit', ['template_info' => $info]);
	}

	/**
	 * 处理编辑模板
	 */
	public function doEdit($id = '')
	{
    
		$info = Request::instance()->post();

		$info['update_time'] = date('Y-m-d H:i:s');
		$info['create_user'] = Session::get('admin_name');

		$template = new TemplateConfig();
		if ($template->save($info, ['id' => $id])) {
		    $action = '短信邮件模板ID'.$id;
		    $this->getLog("3",$action);
			$this->success('编辑成功');
		} else {
			$this->error('编辑失败');
		}
	}
}